﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml;
using SomethingSpacial.CustomControls;
using SomethingSpacial.ViewModel;

namespace SomethingSpacial.Views.Default
{
    public partial class BlogRoll : UserControl
    {
        public BlogRoll()
        {
            InitializeComponent();

            MaxCount = 7;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            string URL = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri;
            URL = URL.Substring(0, URL.LastIndexOf('/')) + "/" + BlogRollViewModel.BlogRoleListXml;

            client.DownloadStringAsync(new Uri(URL));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(e.Result))
                {
                    XmlReader feedReader = XmlReader.Create(new StringReader(e.Result));
                    SyndicationFeed feed = SyndicationFeed.Load(feedReader);
                    BlogRollList.ItemsSource = feed.Items.Take(MaxCount);
                }
            }
            catch (Exception) { }
        }

        #region [DependencyProperties]

        public static readonly DependencyProperty MaxCountProperty = DependencyProperty.Register("MaxCount", typeof(int), typeof(BlogRoll), new PropertyMetadata(new PropertyChangedCallback(OnMaxCountChanged)));

        public int MaxCount
        {
            get { return (int)GetValue(MaxCountProperty); }
            set { SetValue(MaxCountProperty, value); }
        }

        private static void OnMaxCountChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            BlogRoll ThisElement = DpObj as BlogRoll;

            if (ThisElement != null)
            {

            }
        }

        public static readonly DependencyProperty DisplayTitleProperty = DependencyProperty.Register("DisplayTitle", typeof(string), typeof(BlogRoll), new PropertyMetadata(new PropertyChangedCallback(OnDisplayTitleChanged)));

        public string DisplayTitle
        {
            get { return (string)GetValue(DisplayTitleProperty); }
            set { SetValue(DisplayTitleProperty, value); }
        }

        private static void OnDisplayTitleChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            BlogRoll ThisElement = DpObj as BlogRoll;

            if (ThisElement != null)
            {
                TextBlock DisplayTitleTarget = (TextBlock)(ThisElement.FindName("DisplayTitleTarget"));
                DisplayTitleTarget.Text = ThisElement.DisplayTitle;
            }
        }

        #endregion

        private void FeedTitle_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((TextBlock)sender).Foreground = new SolidColorBrush(Tools.GetColor(BlogRollViewModel.TextForeColor));
            ((TextBlock)sender).TextDecorations = TextDecorations.Underline;
        }

        private void FeedTitle_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((TextBlock)sender).Foreground = new SolidColorBrush(Colors.Black);
            ((TextBlock)sender).TextDecorations = null;
        }

        private void FeedTitle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Tools.OpenWindow(((TextBlock)(sender)).Tag.ToString());
        } 
    }
}
